#include <REGX52.H>
#include <INTRINS.H>
#include "delay.h"
#include "IR.h"
#include "timer1.h"


unsigned char ADDR;
unsigned char CMD;
unsigned char Speed;
unsigned int Compare;

sbit Moto=P1^3;

void main()
{
	
	IR_Init();
	Timer1_Init();
	
	while(1)
	{
		if(IR_GetCommendFlag() || IR_GetRepeatFlag())
		{
			CMD = IR_GetCommend();
			ADDR = IR_GetAddress();
			
			if(CMD == IR_NUM_1){
				Speed = 1;
			}
			else if(CMD == IR_NUM_2){
				Speed = 2;
			}
			else if(CMD == IR_NUM_3){
				Speed = 3;
			}
			else if(CMD == IR_NUM_0){
				Speed = 0;
			}
			
			if(Speed == 1){Compare = 50;}
			else if(Speed == 2){Compare = 70;}
			else if(Speed == 3){Compare = 100;}
			else if(Speed == 0){Compare = 0;}
		}
	}
}

/**
  * @brief  定时器0中断处理函数模版
  * @param  无
  * @retval 无
  */
void Timer1_Routine(void) interrupt 3
{
	static unsigned int count = 0;
	
	count++;
	if(count < Compare){
		Moto = 1;				//ULN2003输出低电平，电机转动
	}
	else{
		Moto = 0;				//ULN2003输出高电平，电机停止
	}
	if(count >= 100)
		count = 0;
	
	//定时器溢出之后需要重新装载
	TH1 = 0xFF;					//50微秒@11.0592MHz
	TL1 = 0xD2; 				//定时器装载周期为50微秒
}

